package com.guigu.gulimember.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.gulicommon.context.UserContext;
import com.guigu.gulicommon.entity.pojo.res.Res;
import com.guigu.gulicommon.entity.vo.MemberAddressVo;
import com.guigu.gulimember.entity.UmsMemberReceiveAddress;
import com.guigu.gulimember.mapper.UmsMemberReceiveAddressMapper;
import com.guigu.gulimember.service.IUmsMemberReceiveAddressService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static com.guigu.gulicommon.constant.enumes.res.SelectResCode.SELECT_MEMBER_RECEIVE_ADDRESS_LIST_SUCCESS;

/**
 * <p>
 * 会员收货地址 服务实现类
 * </p>
 *
 * @author 陈俊杰
 * @since 2024-08-03
 */
@Service
public class UmsMemberReceiveAddressServiceImpl extends ServiceImpl<UmsMemberReceiveAddressMapper, UmsMemberReceiveAddress> implements IUmsMemberReceiveAddressService {

    /**
     * 获取会员收货地址列表
     */
    @Override
    public Res<MemberAddressVo> getList() {
        //1. 从当前上下文获取当前登录用户的id
        Long id = UserContext.getInstance().getCurrentUser().getId();
        //2. 获取收货地址列表
        List<MemberAddressVo> addressVos = new LambdaQueryChainWrapper<>(baseMapper).eq(UmsMemberReceiveAddress::getMemberId, id)
                .list()
                .stream().map(item -> {
                    MemberAddressVo memberAddressVo = new MemberAddressVo();
                    BeanUtils.copyProperties(item, memberAddressVo);
                    return memberAddressVo;
                }).collect(Collectors.toList());
        return new Res<>(SELECT_MEMBER_RECEIVE_ADDRESS_LIST_SUCCESS, addressVos);
    }
}
